import { _decorator, Component, Label, Node, Sprite, v3 } from 'cc';
import { Tech } from '../entity/Tech';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Building } from '../entity/Buildings';
import { Soldier } from '../entity/Soldier';
import { Advanced } from '../entity/Advanced';
import { Utils } from '../common/Utils';
import StudyApi from '../api/StudyApi';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('UpgradeTechBuilding')
export class UpgradeTechBuilding extends Component {
    @property(Node)
    bg: Node
    @property(Node)
    close: Node
    @property(Label)
    title: Label
    @property(Sprite)
    cover: Sprite
    @property(Label)
    buildName: Label
    @property(Label)
    level: Label

    @property(Label)
    desc: Label

    @property(Label)
    food: Label
    @property(Label)
    wood: Label
    @property(Label)
    mud: Label
    @property(Label)
    iron: Label
    @property(Label)
    time: Label
    @property(Node)
    cancel: Node
    @property(Node)
    upgrade: Node
    @property(Node)
    canNot: Node
    @property(Label)
    canNotLabel: Label
    @property(Node)
    btnPos: Node
    @property(Label)
    resLabel:Label
    @property(Node)
    resLayout:Node

    techData: Tech

    start() {
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.bg.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.cancel.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.upgrade.on(Node.EventType.TOUCH_END, this.onUpgrade, this)
    }

    async setData(tech: Tech) {
        this.techData = tech

        this.title.string = tech.name + '强化'
        this.cover.spriteFrame = await tech.getTechImg()
        this.buildName.string = tech.name
        this.level.string = '当前等级' + tech.level
        this.desc.string = tech.desc
        let advance = Advanced.createByIdAndLevel(tech.id, tech.level)
        this.food.string = advance.food + ''
        this.wood.string = advance.wood + ''
        this.mud.string = advance.mud + ''
        this.iron.string = advance.iron + ''
        this.time.string = Utils.secondToStr(advance.time)

        this.showBtns()
    }

    showBtns() {
        // 满级的情况
        if(this.techData.level >= this.techData.max){
            this.resLabel.node.active = false
            this.resLayout.active = false
            this.canNotLabel.string = '已经满级了'
            this.canNot.position = this.btnPos.position
            return
        }

        // 判断是否有其他的科技正在研究
        let eventList = Tech.getTechStudyEventList()
        if (eventList.length > 0) {
            this.canNotLabel.string = '正在进行其他研究'
            this.canNot.position = this.btnPos.position
            return
        }

        this.cancel.position = v3(-150, this.btnPos.position.y, 0)
        this.upgrade.position = v3(150, this.btnPos.position.y, 0)
    }

    async onUpgrade() {
        await UIManager.instance.showLoading()
        let res = await StudyApi.studying({
            a: this.techData.id
        })
        UIManager.instance.closeLoading()
        this.onClose()
        UserInfo.instance.refreshVillageDetail()
    }

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.UpgradeTechBuilding)
    }


}


